package com.hansen.library.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.IBinder;
import android.support.annotation.StringRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.hansen.library.R;

/**
 * Created by han on 2019/6/28 0028.
 */

public abstract class BaseDialogFragment extends DialogFragment {

    protected Dialog mDialog;

    protected Context mContext;

    private LoadingDialog mLoadingDialog;

    //判断是否需要隐藏软键盘
    protected boolean isShouldHideKeyBoard(View view, MotionEvent ev) {
        if (view != null && (view instanceof EditText)) {
            int[] l = {0, 0};
            view.getLocationInWindow(l);
            int left = l[0];
            int top = l[1];
            int bottom = top + view.getHeight();
            int right = left + view.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top && ev.getY() < bottom) {
                //点击EditText事件，忽略
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }


    //隐藏软键盘
    protected void hideKeyboard(IBinder windowToken) {
        InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        if (im.isActive() && windowToken != null) {
            im.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        Fragment mFragment = manager.findFragmentByTag(tag);
        //防止java.lang.IllegalStateException: Fragment already added
        if (mFragment == null || !mFragment.isAdded()) {
            super.show(manager, tag);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    //显示加载进度
    protected void showLoadingDialog() {
        showLoadingDialog(R.string.text_waiting_loading);
    }

    //显示加载进度
    protected void showLoadingDialog(@StringRes int resId) {
        showLoadingDialog(getString(resId));
    }

    //显示加载进度
    protected void showLoadingDialog(String res) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getActivity())
                    .builder()
                    .setCanceledOnTouchOutside(false)
                    .setCanceled(false);
        }

        mLoadingDialog.setTitle(res);

        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    protected void dismissDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        mDialog = null;
        mContext = null;
        if(mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
        super.onDestroy();
    }
}
